package com.jbh.request.AppApi

import android.util.Log
import com.google.gson.Gson
import com.jbh.libapi.ApiConfig
import com.jbh.libapi.HttpRequest
import com.jbh.request.ApiKZ
import com.jbh.request.i.YxjCallback
import com.jbh.request.response.BaseResponse
import okhttp3.Call
import okhttp3.Callback
import okhttp3.MediaType
import okhttp3.RequestBody
import okhttp3.Response
import rx.Observer
import rx.schedulers.Schedulers
import java.io.IOException

object CommonRequest {

    init {
        ApiConfig.baseUrl = Api.HOST
    }

    const val tag = "[CommonRequest]->"

    /**
     * 公共的请求入口
     *
     */
    fun common(api: String, content: String, callback: Callback) {
        Log.v(tag, "post->$content")
        HttpRequest.post(Api.HOST + api, content, object : Callback {
            override fun onFailure(call: Call, e: IOException) {
                callback.onFailure(call, e)
                Log.v(tag, "onFailure->${e.message}")
            }

            override fun onResponse(call: Call, response: Response) {
                callback.onResponse(call, response)
                Log.v(tag, "onFailure->${java.lang.String(response.body()?.bytes())}")
            }
        })
    }


    /**
     * 公共的请求入口
     */
    fun <T> commonRequest(
        api: String,
        content: String,
        clazz: Class<T>,
        yxjCallback: YxjCallback<T>
    ) {
        Log.v(tag, "post->$content")
        HttpRequest.request(ApiKZ::class.java)
            .apiPost(
                api,
                RequestBody.create(MediaType.parse("application/json; charset=utf-8"), content)
            )
            .subscribeOn(Schedulers.io())
            .subscribeOn(Schedulers.newThread())
            .subscribe(object : Observer<BaseResponse> {
                override fun onCompleted() {
                }

                override fun onError(e: Throwable?) {
                    Log.v(tag, "success->${e?.message}")
                    e?.message?.let { yxjCallback.onFailure(-100, it) }
                }

                override fun onNext(t: BaseResponse) {
                    Log.v(tag, "success->${t.d}")
                    val resp: com.jbh.request.response.Response = Gson().fromJson(
                        Gson().toJson(t.d),
                        com.jbh.request.response.Response::class.java
                    )
                    if (resp.Code == 0) {
                        yxjCallback.onSuccess(objectFormat(t.d, clazz))
                    } else {
                        yxjCallback.onFailure(resp.Code, resp.Msg)
                    }
                }
            })

//        val call = HttpRequest.request(ApiKZ::class.java)
//            .apiPost1(
//                api,
//                RequestBody.create(MediaType.parse("application/json; charset=utf-8"), content)
//            )
//        call.enqueue(object : retrofit2.Callback<BaseResponse> {
//            override fun onResponse(
//                call: retrofit2.Call<BaseResponse>?,
//                response: retrofit2.Response<BaseResponse>?
//            ) {
//                Log.v(tag, "apiPost1->${response?.body()?.d}")
//            }
//
//            override fun onFailure(call: retrofit2.Call<BaseResponse>?, t: Throwable?) {
//                Log.v(tag, "apiPost1->${t?.message}")
//            }
//        })
    }


    /**
     * @param obj
     * @param cls
     */
    fun <T> objectFormat(obj: Any?, cls: Class<T>): T {
        return Gson().fromJson(Gson().toJson(obj), cls)
    }
}


